package StopTrack::Stock;

use strict;
use warnings;

use base 'StopTrack::GEN::Stock';

use Gantry::Plugins::AutoCRUD qw(
    do_add
    do_edit
    do_delete
    form_name
    write_file
);

use StopTrack::Model;
use StopTrack::Model::stock qw( $STOCK );
use StopTrack::Model::lot   qw( $LOT   );
use StopTrack::Model::stop  qw( $STOP  );

use Gantry::Plugins::DBIxClassConn qw( get_schema );

#-----------------------------------------------------------------
# $self->do_report(  )
#-----------------------------------------------------------------
sub do_report {
    my ( $self ) = @_;

    my @retval;

    $self->stash->view->template( 'report.tt' );
    $self->stash->view->title( 'Stock Sumary Report' );

    my $total_cost_basis = 0.0;
    my $total_value      = 0.0;

    my @stocks = $STOCK->gsearch( $self, undef );

    foreach my $stock ( @stocks ) {
        my @lots   = $LOT->gsearch(  $self, { stock => $stock->id } );
        my $stop   = $STOP->gsearch( $self, { stock => $stock->id } )->next;

        my $shares     = 0;
        my $cost_basis = 0.0;

        foreach my $lot ( @lots ) {
            $shares     += $lot->shares;
            $cost_basis += $lot->paid * $lot->shares;
        }

        $cost_basis       += 2 * $self->commission;
        $total_cost_basis += $cost_basis;
        $cost_basis       /= 100.0;  # pennies to dollars

        my $stop_value;

        if ( $stop->actual_price ) {
            $stop_value = $stop->actual_price * $shares;
        }
        else {
            $stop_value = $stop->stop_price   * $shares;
        }

        $total_value   += $stop_value;
        $stop_value    /= 100.0;

        my $profit = $stop_value - $cost_basis;

        push @retval, {
            name       => $stock->name,
            shares     => $shares,
            cost_basis => $cost_basis,
            profit     => $profit,
        };

    }

    my $total_profit   = ( $total_value - $total_cost_basis ) / 100.0;
    $total_cost_basis /= 100.0;

    $self->stash->view->data( {
        stocks           => \@retval,
        total_profit     => $total_profit,
        total_cost_basis => $total_cost_basis,
    } );
}

#-----------------------------------------------------------------
# $self->do_main(  )
#-----------------------------------------------------------------
# This method supplied by StopTrack::GEN::Stock

#-----------------------------------------------------------------
# $self->form( $row )
#-----------------------------------------------------------------
# This method supplied by StopTrack::GEN::Stock

sub schema_base_class { return 'StopTrack::Model'; }

#-----------------------------------------------------------------
# get_model_name( )
#-----------------------------------------------------------------
sub get_model_name {
    return $STOCK;
}

#-----------------------------------------------------------------
# get_orm_helper( )
#-----------------------------------------------------------------
sub get_orm_helper {
    return 'Gantry::Plugins::AutoCRUDHelper::DBIxClass';
}

#-----------------------------------------------------------------
# text_descr( )
#-----------------------------------------------------------------
sub text_descr     {
    return 'stock';
}

1;

=head1 NAME

StopTrack::Stock - A controller in the StopTrack application

=head1 SYNOPSIS

This package is meant to be used in a stand alone server/CGI script or the
Perl block of an httpd.conf file.

Stand Alone Server or CGI script:

    use StopTrack::Stock;

    my $cgi = Gantry::Engine::CGI->new( {
        config => {
            #...
        },
        locations => {
            '/someurl' => 'StopTrack::Stock',
            #...
        },
    } );

httpd.conf:

    <Perl>
        # ...
        use StopTrack::Stock;
    </Perl>

    <Location /someurl>
        SetHandler  perl-script
        PerlHandler StopTrack::Stock
    </Location>

If all went well, one of these was correctly written during app generation.

=head1 DESCRIPTION

This module was originally generated by Bigtop.  But feel free to edit it.
You might even want to describe the table this module controls here.

=head1 METHODS

=over 4

=item get_model_name

=item text_descr

=item schema_base_class

=item get_orm_helper


=back


=head1 METHODS INHERITED FROM StopTrack::GEN::Stock

=over 4

=item do_main

=item form


=back


=head1 DEPENDENCIES

    StopTrack
    StopTrack::GEN::Stock
    StopTrack::Model::stock
    Gantry::Plugins::AutoCRUD

=head1 AUTHOR

Philip Crow, E<lt>phil@apple.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007 Philip Crow

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.6 or,
at your option, any later version of Perl 5 you may have available.

=cut
